package imnu.th.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: lys
 * @类名: Bank
 * @Title:
 * @date: 2020/7/22
 **/
public class Bank {
    private int account = 100;
    private final ReentrantLock lock = new ReentrantLock();
    //取钱
    public int getMonet(int order){
        this.lock.lock();
        try {
            System.out.println("尊敬的" + Thread.currentThread().getName() +
                    "用户，您余额为" + account + "元。");
            if (account >= order) {

                System.out.println("系统正在处理，请稍等。。。");
                try {
                    Thread.sleep((long) (Math.random() * 2000 + 600));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                account -= order;
                System.out.println("尊敬的" + Thread.currentThread().getName() +
                        "用户，您本次的取款金额为" + order + "元,当前用户余额为" + account + "元。");
                System.out.println();
                return account;
            } else {
                System.out.println("对不起" + Thread.currentThread().getName() + ",您的余额不足。。。");
                return 0;
            }
        }finally {
            this.lock.unlock();
        }


    }

    //咨询业务
    public void ask(){
        this.lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "正在咨询，请稍后。。。");
            try {
                Thread.sleep((long) (Math.random() * 2000 + 600));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "业务咨询完毕。");
            System.out.println();
        }finally {
            this.lock.unlock();
        }
    }
}
